package link.gpt.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import link.gpt.dao.UserDao;
import link.gpt.pojo.User;
import link.gpt.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @version V1.0A
 * @uthor gpt
 * @Project holiday_task
 * @Package link.gpt.service.impl
 * @Description: ${TODO}
 * @Date 2021/10/7 15:06
 */
@Service
public class UserServiceImpl implements UserService {


   @Autowired
   private UserDao userDao;


   @Override
   public User login(String name, String password) {

      User user = userDao.login(name, password);

      if (user == null) {
         throw new RuntimeException("用户名不存在或密码错误!");
      }


      if (user.getStatus() == 0) {
         throw new RuntimeException("账户被冻结!");
      }

      return user;
   }

   @Override
   public PageInfo<User> findAllByNameAndPage(String name, int num, int size) {

      PageHelper.startPage(num, size);
      List<User> userList = userDao.findAllByNameAndPage(name);
      return new PageInfo<>(userList);
   }

   @Override
   public int modifyPassword(Integer id, String oldPassword, String newPassword) {
      User user = userDao.selectByPrimaryKey(id);
      if (user == null) {
         throw new RuntimeException("用户名不存在!");
      }

      if (!user.getPassword().equals(oldPassword)) {
         throw new RuntimeException("旧密码错误!");
      }
      user = new User();
      user.setId(id);
      user.setPassword(newPassword);
      int count = userDao.updateByPrimaryKeySelective(user);
      return count;
   }
}
